package com.baoming.service.impl;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baoming.dao.IPropertiesDao;
import com.baoming.domain.Properties;
import com.baoming.service.IPropertiesService;

@Transactional(readOnly=true)
@Service(IPropertiesService.SERVICE)
public class PropertiesServiceImpl implements IPropertiesService {

	@Resource(name=IPropertiesDao.SERVICE)
	IPropertiesDao propDao ; 
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void saveProperties(List<Properties> properties){
		 
		 propDao.saveObjectByCollection(properties) ; 
	}
	  
	
	/**
	 * 得到所有属性值 
	 */
	@SuppressWarnings("unchecked")
	public List<Properties> listProperties() {

		ServletContext servletContext = ServletActionContext.getServletContext();
		String realPath = servletContext.getRealPath("/WEB-INF/classes/properties.xml");
		
		File file = new File(realPath);
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(file);
			Element element = document.getRootElement();
			List<Properties> propertiesList = new ArrayList<Properties>() ; 
			
			for (Iterator<Element> iter = element.elementIterator("property"); iter.hasNext();) { 
				Element xmlElement = iter.next();
				String name = xmlElement.elementText("key");
				String value = xmlElement.elementText("value"); 
				Properties p = new Properties() ; 
				p.setName(name) ; 
				p.setValue(value) ; 
				propertiesList.add(p) ; 
				 
			}
			return propertiesList ; 
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return null;
	}


	public List<Properties> getPropertiesBySingupId(String singupId) {
		String hql = " and m.singupId = ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(singupId) ; 
		return propDao.findCollectionByConditionNoPage(hql, params.toArray(), null) ; 
	}


	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void deleteProperties(List<Properties> properties) {
		propDao.deleteObjectByCollection(properties) ; 
	}


	@SuppressWarnings("unchecked")
	public String findSingProp(String prop , String xmlSource) {
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(new StringReader(xmlSource));
			Element element = document.getRootElement(); 
			for (Iterator<Element> iter = element.elementIterator("property"); iter.hasNext();) { 
				Element xmlElement = iter.next();
				String name = xmlElement.elementText("name");
				if(name.equals(prop)){
					return  xmlElement.elementText("value");  
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}


	@SuppressWarnings("unchecked")
	public String findSingProp(String prop) {
		ServletContext servletContext = ServletActionContext.getServletContext();
		String realPath = servletContext.getRealPath("/WEB-INF/classes/properties.xml");
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(new File(realPath));
			Element element = document.getRootElement(); 
			for (Iterator<Element> iter = element.elementIterator("property"); iter.hasNext();) { 
				Element xmlElement = iter.next();
				String name = xmlElement.elementText("key");
				if(name.equals(prop)){
					return  xmlElement.elementText("value");  
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}


	public Properties selectPropById(String propId) {
		return propDao.findObjectById(propId);
	}
 
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateProp(Properties prop) {
		propDao.update(prop) ; 
	}
 
	/**
	 * 更新属性值到数据库
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateProp(String valueStr, String propId) {
		propDao.updateXmlValue(valueStr,propId) ; 
	}

	
}
